<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="封面" prop="jsbos_tems_course_jsbos_cover">
              <UploadImg
                v-model="dataForm.jsbos_tems_course_jsbos_cover"
                :fileSize="5"
                sizeUnit="MB"
                :limit="9"
              >
              </UploadImg>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="名称" prop="jsbos_tems_course_jsbos_name">
              <el-input
                v-model="dataForm.jsbos_tems_course_jsbos_name"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="星级评分"
              prop="jsbos_tems_course_jsbos_starScore"
            >
              <el-input-number
                v-model="dataForm.jsbos_tems_course_jsbos_starScore"
                placeholder="数字文本"
                :step="1"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="评分人数"
              prop="jsbos_tems_course_jsbos_starUsers"
            >
              <el-input-number
                v-model="dataForm.jsbos_tems_course_jsbos_starUsers"
                placeholder="数字文本"
                :step="1"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="学习人数"
              prop="jsbos_tems_course_jsbos_learnUsers"
            >
              <el-input-number
                v-model="dataForm.jsbos_tems_course_jsbos_learnUsers"
                placeholder="数字文本"
                :step="1"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="收藏数"
              prop="jsbos_tems_course_jsbos_favoriteCount"
            >
              <el-input-number
                v-model="dataForm.jsbos_tems_course_jsbos_favoriteCount"
                placeholder="数字文本"
                :step="1"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="学习进度" prop="process">
              <el-input-number
                v-model="dataForm.process"
                placeholder="数字文本"
                :step="1"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="enabledMark">
              <el-select
                v-model="dataForm.enabledMark"
                placeholder="请选择"
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in enabledMarkOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="讲师" prop="jsbos_tems_course_jsbos_teacher">
              <el-input
                v-model="dataForm.jsbos_tems_course_jsbos_teacher"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否有考试" prop="isHaveExam">
              <el-switch
                v-model="dataForm.isHaveExam"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="考试是否完成" prop="isCompletedExam">
              <el-switch
                v-model="dataForm.isCompletedExam"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否有作业" prop="isHaveHomework">
              <el-switch
                v-model="dataForm.isHaveHomework"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="作业是否完成" prop="isCompletedHomework">
              <el-switch
                v-model="dataForm.isCompletedHomework"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否评分" prop="isHaveStar">
              <el-switch
                v-model="dataForm.isHaveStar"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否完成评分" prop="isCompletedStar">
              <el-switch
                v-model="dataForm.isCompletedStar"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="我的评分" prop="myStar">
              <el-input-number
                v-model="dataForm.myStar"
                placeholder="数字文本"
                :step="1"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="修改时间" prop="lastModifyTime">
              <el-input
                v-model="dataForm.lastModifyTime"
                placeholder="系统自动生成"
                readonly
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否有问卷" prop="isHaveQuestionnaire">
              <el-switch
                v-model="dataForm.isHaveQuestionnaire"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否完成问卷" prop="isCompletedQuestionnaire">
              <el-switch
                v-model="dataForm.isCompletedQuestionnaire"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label-width="0">
              <div class="jsbos-common-title">
                <h2>设计子表</h2>
              </div>
              <el-table :data="dataForm.TemsCourseStudyChapterList" size="mini">
                <el-table-column
                  type="index"
                  width="50"
                  label="序号"
                  align="center"
                />
                <el-table-column prop="process" label="章节学习进度">
                  <template slot-scope="scope">
                    <el-input-number
                      v-model="scope.row.process"
                      placeholder="数字文本"
                      :style="{ width: '100%' }"
                      :step="1"
                    >
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="50">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      class="jsbos-table-delBtn"
                      @click="delTemsCourseStudyChapterList(scope.$index)"
                    >
                      删除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div
                class="table-actions"
                @click="addTemsCourseStudyChapterList()"
              >
                <el-button type="text" icon="el-icon-plus">添加</el-button>
              </div>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        process: "",
        enabledMark: "0",
        isHaveExam: 0,
        isCompletedExam: 0,
        isHaveHomework: 0,
        isCompletedHomework: 0,
        isHaveStar: 0,
        isCompletedStar: 0,
        myStar: "",
        lastModifyTime: "",
        isHaveQuestionnaire: 0,
        isCompletedQuestionnaire: 0,
        temsCourseStudyChapterList: [],
        tems_course: {
          cover: "",
          name: "",
          starScore: "",
          starUsers: "",
          learnUsers: "",
          favoriteCount: "",
          teacher: "",
        },
        jsbos_tems_course_jsbos_cover: "",
        jsbos_tems_course_jsbos_name: "",
        jsbos_tems_course_jsbos_starScore: "",
        jsbos_tems_course_jsbos_starUsers: "",
        jsbos_tems_course_jsbos_learnUsers: "",
        jsbos_tems_course_jsbos_favoriteCount: "",
        jsbos_tems_course_jsbos_teacher: "",
      },
      rules: {},
      enabledMarkOptions: [
        { fullName: "已完成", id: "1" },
        { fullName: "未完成", id: "0" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    temsCourseStudyChapterExist() {
      let isOk = true;
      for (
        let i = 0;
        i < this.dataForm.temsCourseStudyChapterList.length;
        i++
      ) {
        const e = this.dataForm.temsCourseStudyChapterList[i];
      }
      return isOk;
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/course/TemsCourseStudy/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          if (!this.temsCourseStudyChapterExist()) return;
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/course/TemsCourseStudy",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/course/TemsCourseStudy/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    addTemsCourseStudyChapterList() {
      let item = {
        process: undefined,
      };
      this.dataForm.TemsCourseStudyChapterList.push(item);
    },
    delTemsCourseStudyChapterList(index) {
      this.dataForm.TemsCourseStudyChapterList.splice(index, 1);
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.isHaveExam = parseInt(_data.isHaveExam);
      _data.isCompletedExam = parseInt(_data.isCompletedExam);
      _data.isHaveHomework = parseInt(_data.isHaveHomework);
      _data.isCompletedHomework = parseInt(_data.isCompletedHomework);
      _data.isHaveStar = parseInt(_data.isHaveStar);
      _data.isCompletedStar = parseInt(_data.isCompletedStar);
      _data.isHaveQuestionnaire = parseInt(_data.isHaveQuestionnaire);
      _data.isCompletedQuestionnaire = parseInt(_data.isCompletedQuestionnaire);
      _data.tems_course.cover = JSON.stringify(
        _data.jsbos_tems_course_jsbos_cover
      );
      _data.tems_course.name = _data.jsbos_tems_course_jsbos_name;
      _data.tems_course.starScore = _data.jsbos_tems_course_jsbos_starScore;
      _data.tems_course.starUsers = _data.jsbos_tems_course_jsbos_starUsers;
      _data.tems_course.learnUsers = _data.jsbos_tems_course_jsbos_learnUsers;
      _data.tems_course.favoriteCount =
        _data.jsbos_tems_course_jsbos_favoriteCount;
      _data.tems_course.teacher = _data.jsbos_tems_course_jsbos_teacher;
      for (let i = 0; i < _data.temsCourseStudyChapterList.length; i++) {
        var _list = _data.temsCourseStudyChapterList[i];
      }
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.isHaveExam = parseInt(_dataAll.isHaveExam);
      _dataAll.isCompletedExam = parseInt(_dataAll.isCompletedExam);
      _dataAll.isHaveHomework = parseInt(_dataAll.isHaveHomework);
      _dataAll.isCompletedHomework = parseInt(_dataAll.isCompletedHomework);
      _dataAll.isHaveStar = parseInt(_dataAll.isHaveStar);
      _dataAll.isCompletedStar = parseInt(_dataAll.isCompletedStar);
      _dataAll.isHaveQuestionnaire = parseInt(_dataAll.isHaveQuestionnaire);
      _dataAll.isCompletedQuestionnaire = parseInt(
        _dataAll.isCompletedQuestionnaire
      );
      _dataAll.jsbos_tems_course_jsbos_cover = JSON.parse(
        _dataAll.tems_course.cover
      );
      _dataAll.jsbos_tems_course_jsbos_name = _dataAll.tems_course.name;
      _dataAll.jsbos_tems_course_jsbos_starScore =
        _dataAll.tems_course.starScore;
      _dataAll.jsbos_tems_course_jsbos_starUsers =
        _dataAll.tems_course.starUsers;
      _dataAll.jsbos_tems_course_jsbos_learnUsers =
        _dataAll.tems_course.learnUsers;
      _dataAll.jsbos_tems_course_jsbos_favoriteCount =
        _dataAll.tems_course.favoriteCount;
      _dataAll.jsbos_tems_course_jsbos_teacher = _dataAll.tems_course.teacher;
      for (let i = 0; i < _dataAll.temsCourseStudyChapterList.length; i++) {
        var _list = _dataAll.temsCourseStudyChapterList[i];
      }
      this.dataForm = _dataAll;
    },
  },
};
</script>
